<?php

namespace app\service;

use app\common\Uploader;
use app\model\SystemUploadfile;
use think\facade\Filesystem;
use think\File;
use think\facade\Request;

class SystemUploadService
{
    public function create($file, $adminId = 0, $path = Uploader::IMAGE_PATH)
    {
        $storagePath = Uploader::PUBLIC_PATH;
        /** @var File $file */
        $saveName = Filesystem::disk($storagePath)->putFile($path, $file);
        $imageUrl = join('', [
            Request::domain(),
            '/' . Uploader::STORAGE_PATH . '/',
            $saveName
        ]);
        $model = SystemUploadfile::create([
            'created_by' => $adminId,
            'storage_model' => 1,
            'origin_name' => $file->getOriginalName(),
            'storage_path' => $storagePath,
            'mime_type' => $file->getMime(),
            'suffix' => $file->extension(),
            'size_byte' => $file->getSize(),
            'url' => $saveName,
        ]);
        return [
            'name' => $model->origin_name,
            'id' => $model->id,
            'url' => $imageUrl
        ];
    }
}